package com.lanren.familybookapp.ui.user.presenter;

import android.text.TextUtils;

import com.lanren.common.base.impl.BasePresenter;
import com.lanren.familybookapp.ui.user.interfaces.ILoginModel;
import com.lanren.familybookapp.ui.user.model.LoginModel;

/**
 * @ClassName LoginPresenter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/9/2 15:23
 * @Version 1.0
 */
public class LoginPresenter extends BasePresenter<ILoginModel.ILoginView> {

    private LoginModel loginModel;

    public LoginPresenter() {
        loginModel = new LoginModel();
    }

    /**
     * 验证凭据
     */
    public void validateCredentials(String userName, String password) {
        mRootView.showLoading();
        if (TextUtils.isEmpty(userName)) {
            mRootView.showMessage("用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            mRootView.showMessage("密码不能为空");
            return;
        }
        loginModel.login(userName, password, new ILoginModel.LoginModel() {

            @Override
            public void onError(String data) {
                mRootView.showMessage(data);
            }

            @Override
            public void onSuccess() {
                mRootView.navigateToHome();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (loginModel != null) {
            loginModel.clearDisposable();
            loginModel = null;
        }

    }
}
